package com.llkj.contractmanagement.main;

import android.app.Activity;
import android.app.Application;
import android.os.Handler;

import com.llkj.contractmanagement.model.UserInfoBean;
import com.llkj.contractmanagement.utils.ImageLoaderUtil;
import com.mob.MobSDK;
import com.zhy.http.okhttp.OkHttpUtils;

import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import cn.jpush.android.api.JPushInterface;


/**
 *
 */
public class MyApplication extends Application {
    private List<Activity> activitys = new LinkedList<>();
    private static MyApplication instance;
    public static Handler handler = new Handler();

    @Override
    public void onCreate() {
        super.onCreate();
        instance = this;
        JPushInterface.setDebugMode(true);
        JPushInterface.init(this);
        ImageLoaderUtil.initConfig(this);
        UserInfoBean.getInstance().getAccess();
        OkHttpUtils.getInstance().setConnectTimeout(10000, TimeUnit.MILLISECONDS);

        // ShareSDK
        MobSDK.init(this, "16c1508f5a99c", "696f20b80ce8e72c66b54ba66111d6ee");
    }

    public static MyApplication getInstance() {
        return instance;
    }

    /**
     * 添加Activity到容器中
     *
     * @param activity
     */
    public void addActivity(Activity activity) {
        if (activitys != null) {
            if (!activitys.contains(activity)) {
                activitys.add(activity);
            }
        }
    }

    /**
     * 移除Activity
     *
     * @param activity
     */
    public void removeActivity(Activity activity) {
        if (activitys != null && activitys.size() > 0) {
            if (activitys.contains(activity)) {
                activitys.remove(activity);
            }
        }
    }

    /**
     * 销毁activity
     */
    public void exit() {
        if (activitys != null && activitys.size() > 0) {
            for (Activity activity : activitys) {
                activity.finish();
            }
            activitys.clear();
        }
    }


    /**
     * 根据类名结束指定的activity
     *
     * @param cls
     */
    public void finishSingleActivity(Class cls) {
        Activity tempActivity = null;
        for (Activity activity : activitys) {
            if (activity.getClass().equals(cls)) {
                tempActivity = activity;
            }
        }
        if (tempActivity != null) {
            tempActivity.finish();
        }
    }


}
